//
//  XS_IntegralVC.swift
//  XueDao
//
//  Created by Mac on 2020/12/24.
//

import UIKit

class XS_IntegralVC: XS_BaseVC {

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var accountLabel: UILabel!
    
    var dataModel = XS_IntegralListM()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        title = "我的学到币"
        shouldNavBarHidden = true
        isStatusBarBlack = false
        loadCustomBar()
        tableView.register(UINib.init(nibName: "XS_IntegralListCell", bundle: Bundle.main), forCellReuseIdentifier: "XS_IntegralListCell")
        tableView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
        view.addSubview(ruleBtn)
        accountLabel.text = XS_Singleton.shared.infoModel?.userBO.money
        tableView.mj_header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(headerRefesh))
        tableView.mj_footer = MJRefreshBackNormalFooter.init(refreshingTarget: self, refreshingAction: #selector(footerRefesh))
        headerRefesh()
    }
    
    lazy var ruleBtn : UIButton = {
        let ruleBtn = UIButton.init(frame: CGRect.init(x: .screenWidth - 90, y: .barHeight, width: 80, height: 44))
        ruleBtn.setTitle("学到币规则", for: .normal)
        ruleBtn.setTitleColor(.white, for: .normal)
        ruleBtn.titleLabel?.font = .systemFont(ofSize: 15)
        ruleBtn.addTarget(self, action: #selector(integralRuleAction), for: .touchUpInside)
        return ruleBtn
    }()
    
    func requestData(){
//
    }
    
    @objc func headerRefesh(){
        requestData(true)
    }
    
    @objc func footerRefesh(){
        requestData(false)
    }
    
    func requestData(_ isFirst: Bool = false){
        if isFirst {
            dataModel.current = 0
            dataModel.records.removeAll()
            
            XS_NetWork.requestJson("/user/getCurrUser")
                .mapModel(type: XS_UserBOM.self) // 返回 模型对象 和原始数据
                .subscribe(onNext: { model, resp in
                    XS_Singleton.shared.infoModel?.userBO = model!
                    self.accountLabel.text = XS_Singleton.shared.infoModel?.userBO.money
                }).disposed(by: disposeBag)
        }
        
        XS_NetWork.requestJson("/money/getUserMoneyOrder", parameters: ["pageNo" : dataModel.current+1, "pageSize" : dataModel.size])
            .mapModel(type: XS_IntegralListM.self) // 返回 模型数组 和原始数据
            .subscribe(onNext: { [weak self] model, resp in
                guard let `self` = self, resp.success, let model = model else { return }
                model.records = self.dataModel.records + model.records
                self.dataModel = model
                self.tableView.reloadData()
                self.tableView.mj_header?.endRefreshing()
                if model.current >= model.pages { // 没有更多
                    self.tableView.mj_footer?.endRefreshingWithNoMoreData()
                }else{
                    self.tableView.mj_footer?.endRefreshing()
                }
            }).disposed(by: disposeBag)
    }


    @IBAction func getIntegralBtnAction(_ sender: UIButton) {
        let getVC = XS_IntegralGetVC()
        navigationController?.pushViewController(getVC, animated: true)
//        let getVC = XS_RechargeVC()
//        navigationController?.pushViewController(getVC, animated: true)
    }
    
    @objc func integralRuleAction(){
//        let bundleStr = Bundle.main.url(forResource: "integral_rule", withExtension: "html")!
//        let webVC = XS_WebPageVC.init(fileURL: bundleStr, allowingReadAccessTo: Bundle.main.bundleURL)
//        navigationController?.pushViewController(webVC, animated: true)
        let webVC = XS_WebPageVC()
        webVC.loadUrl("http://xdapp.yaoxuedao.com.cn/integral")
        navigationController?.pushViewController(webVC, animated: true)
    }

}

extension XS_IntegralVC : UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataModel.records.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "XS_IntegralListCell", for: indexPath) as! XS_IntegralListCell
        if indexPath.row < dataModel.records.count {
            cell.model = dataModel.records[indexPath.row]
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70
    }
    
}
